import Scanner from "./Scanner";
import nestTokens from "./nestTokens";
import myNestTokens from "./myNestTokens"
/*
    将模板字符串变为tokens数组
*/

export default function parseTemplateToTokens(templateStr) {
    const tokens = [];
    const scanner = new Scanner(templateStr);
    console.log("-> templateStr", templateStr);
    let words;

    while (!scanner.eos()) {
        words = scanner.scanUtil('{{');
        tokens.push(['text', words]);
        scanner.scan("{{");
        words = scanner.scanUtil('}}');
        if (words !== '') {
            // 这里面的 words 是 {{ }} 内的内容
            if (words[0] === '#') {
                // 如果是数组 存入
                tokens.push(['#', words.substring(1)]);
            }else if(words[0] === '/'){
                // 如果是 / 存入
                tokens.push(['/', words.substring(1)]);
            }else {
                tokens.push(['name',words])
            }
        }
        scanner.scan("}}");
    }
    return myNestTokens(tokens)
}
